package com.woniuxy.oldobject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldobject.dto.StaffDto;
import com.woniuxy.oldobject.entity.ResponseEntity;
import com.woniuxy.oldobject.entity.Staff;
import com.woniuxy.oldobject.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 员工管理接口
 * @Title: StaffController
 * @Package com.woniuxy.oldobject.controller
 * @Author: LSL
 * @CreateTime: 2022/7/25 15:52
 */
@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    StaffService staffService;


    /**
     * 通过id查询对应员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/queryStaffById")
    public ResponseEntity queryStaffById(int id) {
        Staff staff = staffService.queryById(id);
        if (staff != null) {
            return new ResponseEntity(staff);
        }
        return ResponseEntity.ERROR;
    }


    /**
     * 条件查询护工信息并分页
     *
     * @param staffDto
     * @return
     */
    @PostMapping("/queryAllStaffByLimit")
    public ResponseEntity queryAllStaffByLimit(@RequestBody StaffDto staffDto) {
        staffDto.setOlder(1);
        PageHelper.startPage(staffDto.getPageSize(), staffDto.getPageNum());
        List<Staff> staff = staffService.queryAllByLimitPgaeInfo(staffDto);
        if (staff != null) {
            PageInfo<Staff> list = new PageInfo<>(staff);
            return new ResponseEntity(list);
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 通过id查询护工信息并加上负责老人姓名
     *
     * @param id
     * @return
     */
    @GetMapping("/queryStaffWithOlders")
    public ResponseEntity queryStaffWithOlders(int id) {
        Staff staff = staffService.queryStaffWithOlders(id);
        if (staff != null) {
            return new ResponseEntity(staff);
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 查询护工信息并加上负责老人姓名
     *
     * @return
     */
    @GetMapping("/queryStaffWithOldersNoId")
    public ResponseEntity queryStaffWithOldersNoId() {

        List<Staff> list = staffService.queryStaffWithOldersNoId();
        if (list != null) {
            return new ResponseEntity(list);
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 条件查询未分配老人的员工信息并分页
     *
     * @param staffDto
     * @return
     */
    @PostMapping("/queryAllStaffNoOlder")
    public ResponseEntity queryAllStaffNoOlder(@RequestBody StaffDto staffDto) {
        PageHelper.startPage(staffDto.getPageSize(), staffDto.getPageNum());
        List<Staff> staff = staffService.queryAllByLimitPgaeInfo(staffDto);
        if (staff != null) {
            PageInfo<Staff> list = new PageInfo<>(staff);
            return new ResponseEntity(list);
        }
        return ResponseEntity.ERROR;
    }

    /**
     * 批量修改
     * @param staffDto
     * @return 是否修改成功
     */
    @PostMapping("/updatebatah")
    public ResponseEntity updatebath(@RequestBody StaffDto staffDto) {
        boolean falg = staffService.updates(staffDto);
        if (falg) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    //修改 是否分配老人
    @PostMapping("/updateByOlder")
    public ResponseEntity updateByOlder(@RequestBody StaffDto staffDto) {
        boolean falg = staffService.updateByOlder(staffDto);
        if (falg) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.ERROR;
        }
    }

    @RequestMapping("/list")
    //查询员工列表不带条件
    public ResponseEntity findAllStaff() {
        List<Staff> list = staffService.SelectAll();
        if (list != null) {
            return new ResponseEntity(200, "查询员工列表成功", list);
        } else {
            return ResponseEntity.ERROR;
        }
    }
}
